package com.diandian.backend.dingding.exchange.controller;

import com.diandian.backend.dingding.exchange.service.DingdingTokenService;
import com.diandian.base.boot.starter.response.BaseResult;
import com.diandian.base.boot.starter.util.ResultUtil;
import com.diandian.dingding.boot.starter.service.DingdingApplicationAddressBookRoleManageService;
import com.dingtalk.api.response.OapiRoleGetroleResponse;
import com.dingtalk.api.response.OapiRoleGetrolegroupResponse;
import com.dingtalk.api.response.OapiRoleListResponse;
import com.dingtalk.api.response.OapiRoleSimplelistResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/role")
public class DingdingApplicationAddressBookRoleController {

    private static final Logger logger = LoggerFactory.getLogger(DingdingApplicationAddressBookRoleController.class);

    @Autowired
    private DingdingApplicationAddressBookRoleManageService dingdingApplicationAddressBookService;

    @Autowired
    private DingdingTokenService dingdingTokenService;

    /**
     * 获取角色组列表
     * @return
     */
    @GetMapping("/getRoleGroupList")
    public OapiRoleGetrolegroupResponse getRoleGroupList(@RequestHeader(value = "appKey") String appKey,
                                                         @RequestHeader(value = "appSecret") String appSecret,
                                                         @RequestParam(value = "groupId") Long groupId) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return dingdingApplicationAddressBookService.getRoleGroupList(token, groupId);
    }

    /**
     * 获取角色列表
     * @return
     */
    @GetMapping("/getPageRoleList")
    public OapiRoleListResponse getPageRoleList(@RequestHeader(value = "appKey") String appKey,
                                            @RequestHeader(value = "appSecret") String appSecret,
                                            @RequestParam(value = "offset", required = false) Long offset,
                                            @RequestParam(value = "size", required = false) Long size) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return dingdingApplicationAddressBookService.getPageRoleList(token, offset, size);
    }

    /**
     * 获取角色列表
     * @return
     */
    @GetMapping("/getAllRoleList")
    public BaseResult getAllRoleList(@RequestHeader(value = "appKey") String appKey,
                                                                   @RequestHeader(value = "appSecret") String appSecret) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return ResultUtil.makeResult(dingdingApplicationAddressBookService.getAllRoleList(token));
    }

    /**
     * 获取角色详情
     * @return
     */
    @GetMapping("/getRoleDetail")
    public OapiRoleGetroleResponse getRoleDetail(@RequestHeader(value = "appKey") String appKey,
                                                 @RequestHeader(value = "appSecret") String appSecret,
                                                 @RequestParam(value = "roleId") Long roleId) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return dingdingApplicationAddressBookService.getRoleDetail(token, roleId);
    }

    /**
     * 获取指定角色的员工列表
     * @return
     */
    @GetMapping("/getUserPageByRoleId")
    public OapiRoleSimplelistResponse getUserPageByRoleId(@RequestHeader(value = "appKey") String appKey,
                                                          @RequestHeader(value = "appSecret") String appSecret,
                                                          @RequestParam(value = "roleId") Long roleId,
                                                          @RequestParam(value = "offset", required = false) Long offset,
                                                          @RequestParam(value = "size", required = false) Long size) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return dingdingApplicationAddressBookService.getUserPageByRoleId(token, roleId, offset, size);
    }

    /**
     * 获取指定角色的员工列表
     * @return
     */
    @GetMapping("/getAllUserByRoleId")
    public BaseResult getAllUserByRoleId(@RequestHeader(value = "appKey") String appKey,
                                         @RequestHeader(value = "appSecret") String appSecret,
                                         @RequestParam(value = "roleId") Long roleId) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return ResultUtil.makeResult(dingdingApplicationAddressBookService.getAllUserByRoleId(token, roleId));
    }

}
